package com.ruoyi.commonService;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Service
public class ThirdPartySmsService {
   // @Value("${sms.third-party.url}")
    private String apiUrl;

    //@Value("${sms.third-party.app-id}")
    private String appId;

   // @Value("${sms.third-party.app-secret}")
    private String appSecret;

    public SmsResponse sendSms(SmsRequest request) {
        RestTemplate restTemplate = new RestTemplate();

        HttpHeaders headers = new HttpHeaders();
        headers.set("X-App-Id", appId);
        headers.set("X-App-Secret", appSecret);
        headers.setContentType(MediaType.APPLICATION_JSON);

        Map<String, String> body = new HashMap<>();
        body.put("phone", request.getPhone());
        body.put("content", request.getContent());

        HttpEntity<Map<String, String>> entity =
            new HttpEntity<>(body, headers);

        try {
            return restTemplate.postForObject(
                apiUrl, entity, SmsResponse.class);
        } catch (Exception e) {
            return new SmsResponse(false, "接口调用异常");
        }
    }
}
